package com.jltx.xq.relaxnewspro.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jltx.xq.relaxnewspro.R;
import com.jltx.xq.relaxnewspro.database.collection.ICache;
import com.jltx.xq.relaxnewspro.utils.BookBean;
import com.jltx.xq.relaxnewspro.utils.HttpUtil;
import com.jltx.xq.relaxnewspro.utils.Settings;
import com.jltx.xq.relaxnewspro.utils.Utils;


/**
 * Created by bo on 2017/5/1.
 */

public class ReadingAdapter extends BaseListAdapter<BookBean, ReadingAdapter.ViewHolder> {
    public ReadingAdapter(Context context, ICache<BookBean> cache) {
        super(context, cache);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_reading,parent,false);
        ViewHolder vh = new ViewHolder(itemView);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        BookBean bookBean = getItem(position);
        holder.title.setText(bookBean.getTitle());
        holder.info.setText(bookBean.getInfo());

        if(Settings.noPicMode && HttpUtil.isWIFI == false){
            holder.image.setImageURI(null);
        }else {
            holder.image.setImageURI(Uri.parse(bookBean.getImage()));
        }

        holder.parentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        if(Utils.hasString(bookBean.getEbook_url())) {
            holder.ebook.setVisibility(View.VISIBLE);
        }
        else {
            holder.ebook.setVisibility(View.GONE);
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder{

        private View parentView;
        private SimpleDraweeView image;
        private TextView title;
        private TextView info;
        private CheckBox collect_cb;
        private TextView text;
        private ImageView ebook;

        public ViewHolder(View itemView) {
            super(itemView);
            parentView = itemView;
            image = (SimpleDraweeView) itemView.findViewById(R.id.bookImg);
            title = (TextView) itemView.findViewById(R.id.bookTitle);
            info = (TextView) itemView.findViewById(R.id.bookInfo);
            collect_cb = (CheckBox) itemView.findViewById(R.id.collect_cb);
            ebook = (ImageView) itemView.findViewById(R.id.ebook);
            if(isCollection) {
                text = (TextView) parentView.findViewById(R.id.text);
            }
        }
    }
}
